package com.example.ymm.modules1.controller.sys;

import com.alibaba.fastjson.JSONObject;
import com.example.ymm.model.CommonResult;
import com.example.ymm.modules1.controller.BaseController;
import com.example.ymm.modules1.entity.my.sys.SysMenu;
import com.example.ymm.modules1.entity.my.sys.SysUser;
import com.example.ymm.modules1.service.sys.SysMenuService;
import com.example.ymm.modules1.service.sys.SysUserService;
import com.example.ymm.util.RedisUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/hl/sys/userInfo")
@Tag(name= "pc登录用户信息接口")
@Slf4j
@CrossOrigin
public class SysUserInfoController extends BaseController {
    @Value("${jwt.tokenHeader}")
    private String tokenHeader;
    @Value("${jwt.tokenHead}")
    private String tokenHead;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private SysUserService sysUserService;

    @Autowired
    SysMenuService sysMenuService;


    @RequestMapping(value = "/getUserInfo", method = RequestMethod.POST)
    @Operation(summary= "获取登录者信息")
    public CommonResult getUserInfo() {
        try {
            String userId = getUserId();
            SysUser sysUser = sysUserService.getById(userId);
            return CommonResult.success(sysUser);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }


    }

    @RequestMapping(value = "/getIsLogin", method = RequestMethod.POST)
    @Operation(summary= "是否登录")
    public CommonResult getIsLogin() {
        try {
            String userId = getUserId();
            String userRole = getUserRole();
            JSONObject json=new JSONObject();
            json.put("userId",userId);
            json.put("userName",getUserName());
            json.put("role",userRole);
            String homePath="/dashboard/analysis";
            if("ms_user".equals(userRole)){
                homePath="/dashboard/analysis2";
            }
            json.put("homePath",homePath);


            return CommonResult.success(json);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }


    }

    @RequestMapping(value = "/getUserMenus", method = RequestMethod.POST)
    @Operation(summary= "获取登陆者菜单")
    public CommonResult getUserMenus() {
        try {
            List<SysMenu> userMenus = sysMenuService.getUserMenus();
            return CommonResult.success(userMenus);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }


    }
}
